<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

function manage_plant_add_page() {
    $temp = drupal_get_form('plant_edit_form');
    return $temp;
}

function manage_plant_edit_page($id) {
    $temp = drupal_get_form('plant_edit_form', $id);
    return $temp;
}

function plant_edit_form($form, &$form_state) {
    $id = isset($form_state['build_info']['args'][0]) ? $form_state['build_info']['args'][0] : NULL;
    if (!empty($id)) {
        $record = nonghua_data_load('plant', 'plant_id', $id);
        if (empty($record)) {
            drupal_set_message(t('作物信息不存在！'), 'status', FALSE);
            drupal_goto('manage_plant/list');
        }
    }

    $form['plant_name'] = array(
        '#type' => 'textfield',
        '#size' => 32,
        '#maxlength' => 10,
        '#required' => TRUE,
        '#default_value' => !empty($record) ? $record->plant_name:'',
    );
    $form['plant_category'] = _insert_plant_category_selection_form($form, $form_state,!empty($record) ? $record->plant_category_id:'');


    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => isset($record) ? '修改' : '添加',
        '#access' => plant_edit_form_submit_access($form_state, $id),
    );
    return $form;
}

function plant_edit_form_validate($form, &$form_state) {
    if( !isset($form_state['build_info']['args'][0])  || empty($form_state['build_info']['args'][0])){
        if(!empty($form_state['values']['plant_name'])){
            $query = db_select('plant', 'P');
            $query->condition('P.plant_name',$form_state['values']['plant_name']);
            $query->fields('P');
            $record = $query->execute()->fetchObject();
            if(!empty($record)){
                form_set_error('plant_name', '该作物名称 已存在。');
            }
        }

    }
    if(empty($form_state['values']['plant_category'])){
        form_set_error('plant_category', '必须填写 作物类别。');
    }
}

function plant_edit_form_submit($form, &$form_state) {
    global $user;
    $id = isset($form_state['build_info']['args'][0]) ? $form_state['build_info']['args'][0] : NULL;
    if (empty($id)) { //add new plant
        $nid = db_insert('plant') // Table name no longer needs {}
        ->fields(array(
            'plant_name' => $form_state['values']['plant_name'],
            'plant_category_id' => $form_state['values']['plant_category'],
            'author_uid' => $user->uid,
            'timestamp' => time(),))
            ->execute();
        if ($nid > 0) {
            drupal_set_message(t('添加成功！'), 'status', FALSE);
        }
        clear_search_autocomplete_cach();
        drupal_goto('manage_plant/list');
    } else {
        $nid = db_update('plant')
        ->fields(array(
            'plant_name' => $form_state['values']['plant_name'],
            'plant_category_id' => $form_state['values']['plant_category'],
            'timestamp' => time(),))
            -> condition('plant_id',$id, '=')
            ->execute();
        if ($nid > 0) {
            drupal_set_message(t('修改成功！'), 'status', FALSE);
        }
        clear_search_autocomplete_cach();
        drupal_goto('manage_plant/list');
    }
}

function plant_edit_form_submit_access(&$form_state) {
    global $user;
    $id = isset($form_state['build_info']['args'][0]) ? $form_state['build_info']['args'][0] : NULL;

    if ($id) {
        //edit
        $plant = nonghua_data_load('plant', 'plant_id', $id);
        if ($plant) {
            $user_info = _get_user_info($user->uid);
            if ($plant->author_uid == $user->uid|| $user_info['role'] === 'administrator') {
                return TRUE;
            } else {
                return FALSE;
            }
        } else {
            drupal_set_message(t('作物信息不存在！'), 'status', FALSE);
            return FALSE;
        }
    } else {
        //add
        return user_access('manage plant');
    }
}
